<?php
/**
 * admin.php - Infinity Administration
 *
 * NMIT BIT Project - Infinity - A 3D Website using WebGL & HTML5 Technology
 * Infinity Grid Version 
 * Version: 0.1.2
 *
 * Author: William Liu <qzliuyuzou@gmail.com> <lyzdev@gmail.com>
 *
 * Required Server Version:
 *  PHP5 or higher
 *  MySQL 5 or higher
 *
 * Required Browser Version:
 *  Google Chrome 9 or higher
 *  Mozilla Firefox 4 Beta or higher
 *  *Apple Safari
 *
 * Copyright (c) 2011, William Liu All rights reserved.
 * Infinity is an open source 3D website project
 * Under Apache License 2.0 
 * Apache License 2.0
 * http://www.lyzsoft.cn
 * http://7cloud.wordpress.com
 *
 * Project Starting date: 19/3/2011
 * Update Date 1: 5/5/2011
 * Update Date 2: 6/5/2011
 * Update Date 3: 7/5/2011
 * Update Date 4: 8/5/2011
 * Update Date 5: 14/5/2011
 * Update Date 6: 15/5/2011
 * Update Date 7: 5/6/2011 - Beta 2
 *
**/

//Initialize session
session_start();

//file include check
define('INCLUDE_CHECK',1);
require_once('connection.php');
require_once('function.php');
require_once('actions.php');

//include_once('verify.php');

//Check database connection
@mysql_connect($db_host, $db_user, $db_pass)
or die("Database Connection Fail!");  
@mysql_select_db($db_name)
or die("The Database is not available!");

//message query
$msgQuery = mysql_query("select * from msg order by id desc");
while ($msgRow = mysql_fetch_array($msgQuery))
{
	$msgList .= adminMsgList($msgRow[content],$msgRow[addtime],$msgRow[avatarID], $msgRow[id]);
	/*	
	print_r ($msgQuery);
	print_r ($msgRow);
	*/
	}

//get message id
/*
$msgIDQuery = mysql_query("select id from msg order by id desc");
while($msgID = mysql_fetch_array($msgIDQuery))
{
	$counter = 1;
	//print_r ();
	echo "ID".$msgID[id]."counter".$counter."<br />";
	$counter ++;
	//echo "<script language=\"JavaScript\">var ";
	}
*/
?>

<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Infinity - Administration</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/global.js"></script>
<link rel="stylesheet" type="text/css" href="style.css"/>


</head>

<body>
<div id="header">
   <div id="logo">
      <h1>
         <a href="#" title="Welcome to Infinity Administration">Infinity Administration</a>
      </h1>
   </div>
</div>

<div id="main">
  <div class="content">
    <div class="clear"></div>
		<?php
        if(isset($_SESSION['username']))  
        {
            //Get Session  
            $username = $_SESSION['username']; 
             
            //SQl query, to get user lvl  
            $userQuery = @mysql_query("select user_level from users "."where username = '$username'")  
            or die("SQL query fail!");
            
            $userRow = mysql_fetch_array($userQuery);  
            //Compare user lvl information between database and Sessipon, if is different, then update Session information
            if($userRow['user_level'] != $_SESSION['user_level'])  
            {  
                $_SESSION['user_level'] = $userRow['user_level'];  
            }  
            //Base on Session value to output different welcome information 
            if($_SESSION['user_level'] == 5)
            {
                echo'                
                    <table class="commentlist" cellspacing="0">
                        <thead class="commentheadbar">
                            <tr>
								<th></th>
                                <th class="msgmanagetitle">Current Push</th>
								<th></th>
                            </tr>
                        </thead>
						<tbody id="msglist" class="msglist">
                        '.$msgList.'
						</tbody>
                        <tfoot class="commentheadbar">
                            <tr>
                                <th style="border-bottom:0px;"></th>
                                <th class="msgmanagetitle" style="border-bottom:0px;">Current Push</th>
								<th style="border-bottom:0px;"></th>
                            </tr>
                        </tfoot>
                    </table>		  
                ';
                
                /*Comment for test
                echo '
                <div id="main">
                  <div class="content">
                    <div class="clear"></div>
                    <div id="msgwrap">
                     '.$msgList.'
                    </div>
                  </div>
                </div>
                ';
                */
                
                //enable delete function
                //echo "<script language=\"JavaScript\">var deleEnabled = true; <//script>";
            }
            if($_SESSION['user_level'] == 1)
            {  
                echo "<div class=\"loginmsg\">Welcome User ".$_SESSION['username']. " " ."back!</div>";  
                echo "<script language=\"JavaScript\">window.setTimeout(\"window.location.href=\'index.php\'\", 3000); </script>";
            }
        }
        else
        {
            echo '
			<!--rewrite style.css-->
			<style type="text/css">
			#login table, tbody, tfoot, tr, th, td{
				padding: 2px;
				border: 0;
				outline: 0;
				text-align: left;
				border-bottom:0px; !important
				}
			</style>
			
			<div id="main">
              <div class="content">
                <body>  
                <form id = "login" name="login" method="post" action="verify.php">  
                <table width="300" border="0" align="center" cellpadding="2" cellspacing="2">  
                   <tr>  
                      <td width="150"><div align="left">User Name:</div></td>  
                      <td width="150"><input type="text" name="username"></td>  
                   </tr>  
                   <tr>
                     <td><div align="left">Password:</div></td>  
                     <td><input type="password" name="password"></td>  
                   </tr>
                </table>  
                <p align="center">  
                <input type="submit" name="Submit" value="Submit">  
                <input type="reset" name="Reset" value="Reset">  
                </p>  
                </form>  
                <div class="clear"></div>
              </div>
            </div>
            ';
        }
        ?>
	</div>
</div>
        
        
<div id="footer">
    <p>Powered by William</p>
    <?php
    if(isset($_SESSION['user_level']))
	{
		if($_SESSION['user_level'] == 5)
		{
			echo "<p><span><a title=\"Logout\" href=\"admin.php?action=logout\">Logout</a> | 
			               <a title=\"Logout\" href=\"index.php\">Back to Front</a> | 
						   <a title=\"Add user\" href=\"admin.php?action=reg\">Add user</span></p>";		
			}
		if($_SESSION['user_level'] == 1)
		{
			echo "<p><span><a title=\"Logout\" href=\"admin.php?action=logout\">Logout</a></p>";
			}
	}
	else
	{
		echo "<p><span><a title=\"Oops, are you lost?\" href=\"index.php\"><- Back to Front page</a></span></p>";
		}
	?>
</div>
</body>
</html>